/**
 * New node file
 */

//Google Map api functionality
function MapCtrl($scope) {
	var geocoder= new google.maps.Geocoder();
	var infowindow = new google.maps.InfoWindow();
	
	var mapOptions = {
			zoom: 8,
			center: new google.maps.LatLng(39.9500, -75.1667),
			mapTypeId: google.maps.MapTypeId.TERRAIN
	}
	$scope.map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

	$scope.codeAddress = function() {
		var address = document.getElementById("address").value;
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				$scope.map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
					map: $scope.map,
					position: results[0].geometry.location
				});
			} else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		});
	}

	$scope.barSearch = function() {
		var mapLoc = $scope.map.getCenter();
		var request = {
				location: mapLoc,
				radius: '500',
				types: ['bar']
		};
		service = new google.maps.places.PlacesService($scope.map);
		service.nearbySearch(request, $scope.callback);
	}

	$scope.callback = function(results, status) {
		if (status == google.maps.places.PlacesServiceStatus.OK) {
			for (var i = 0; i < results.length; i++) {
				var place = results[i];
				$scope.createMarker(results[i]);
			}
		}
	}
	
	$scope.createMarker = function(place) {
		var placeLoc = place.geometry.location;
		var marker = new google.maps.Marker({
			map: $scope.map,
			position: place.geometry.location
		});
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.setContent(place.name);
			infowindow.open($scope.map, this);
		});
	}
}

//Managing the poll list
function PollListCtrl($scope, Poll) {
	$scope.polls = Poll.query();
}

//Voting / viewing poll results
function PollItemCtrl($scope, $routeParams, socket, Poll) { 
	$scope.poll = Poll.get({pollId: $routeParams.pollId});
	socket.on('myvote', function(data) {
		console.dir(data);
		if(data._id === $routeParams.pollId) {
			$scope.poll = data;
		}
	});
	socket.on('vote', function(data) {
		alert('attempting to vote');
		console.dir(data);
		if(data._id === $routeParams.pollId) {
			$scope.poll.choices = data.choices;
			$scope.poll.totalVotes = data.totalVotes;
		}   
	});
	$scope.vote = function() {
		var pollId = $scope.poll._id,
		choiceId = $scope.poll.userVote;
		if(choiceId) {
			var voteObj = { poll_id: pollId, choice: choiceId };
			socket.emit('send:vote', voteObj);
		} else {
			alert('You must select an option to vote for');
		}
	};
	
	//TODO
	$scope.deletePoll = function() {
		Poll.remove({ id:$scope.poll._id }, function (err) {
			  if (err) {
				  alert(err);
				  return handleError(err);
			  }
			  alert('deleted (?)');
			});
		alert('Not Yet Implmented');
	};
}

function PollNewCtrl($scope, $location, Poll) {
	$scope.poll = {
			question: '',
			choices: [ { text: '' }, { text: '' }, { text: '' }]
	};  
	$scope.addChoice = function() {
		$scope.poll.choices.push({ text: '' });
	};
	$scope.createPoll = function() {
		var poll = $scope.poll;
		if(poll.question.length > 0) {
			var choiceCount = 0;
			for(var i = 0, ln = poll.choices.length; i < ln; i++) {
				var choice = poll.choices[i];        
				if(choice.text.length > 0) {
					choiceCount++;
				}
			}    
			if(choiceCount > 1) {
				var newPoll = new Poll(poll);  
				newPoll.$save(function(p, resp) {
					if(!p.error) { 
						$location.path('polls');
					} else {
						alert('Could not create poll');
					}
				});
			} else {
				alert('You must enter at least two choices');
			}
		} else {
			alert('You must enter a question');
		}
	};
}